<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2019/11/14
 * Time: 21:27
 */
class ctrl_ali_mq extends L {

    /**
     * 描述 ：在1688上下采购单
     * 参数 ：{
     *      data : {
     *          pddOrder : 拼多多的订单号
     *      }
     * }
     * 作者 ：Zoro.Zhu
    **/
    public static function payOrder($params = null){
        $pddOrder = !empty($params['data']['pddOrder']) ? $params['data']['pddOrder'] : $_GET['pddOrder'];
        $orderInfo = model_order::getOrder(['pddOrder' => $pddOrder]);
        $orderData = json_decode($orderInfo[0]['data'], true);

        $data = [];
        $data['aliPay'] = model_vsAccount::getAccountToken(['account' => '朱大表哥家']);
        $data['warehouse'] = [
            'fullName' => $orderData['receiver_name'],
            'mobile' => '',
            'phone' => $orderData['receiver_phone'],
            'postCode' => '000000',                                             //邮编
            'cityText' => $orderData['city'],
            'provinceText' => $orderData['province'],
            'areaText' => $orderData['town'],
            'townText' => '',
            'address' => $orderData['receiver_address'],
            'districtCode' => '',                                               //地址编码
            'warehouse' => '',
        ];
        $data['purchaseInfo'] = [];
        foreach($orderData['item_list'] as $sku){
            $aliSku = model_aliProduct::getSku($sku['outer_id']);
            $data['purchaseInfo'][] = [
                'offerId' => $aliSku['productId'],
                'specId' => $aliSku['specId'],
                'quantity' => $sku['goods_count'],
            ];
        }
        $data['remark'] = $orderData['remark'];

        $aliOrder = model_api_aliApi::fastCreateOrder($data);
        if(!empty($aliOrder['success'])){
            $count = L::sql("UPDATE 
                `pdd_order` 
            SET 
                `orderState` = 30, 
                `aliOrder` = '{$aliOrder['orderId']}' 
            WHERE 
                `pddOrder` = '{$pddOrder}'");
            if($count){
                //加入获取物流信息的消息队列
                of_base_com_mq::set(['getAliLogistics', $aliOrder['orderId']], ['orderId' => $aliOrder['orderId']]);
                return true;
            }
        }
        return false;
    }

    /**
     * 描述 ：获取订单明细
     * 作者 ：Zoro.Zhu
    **/
    public static function getOrderDetail($params = null){
        $orderId = !empty($params['data']['orderId']) ? $params['data']['orderId'] : $_GET['orderId'];
        $data['aliPay'] = model_vsAccount::getAccountToken(['account' => '朱大表哥家']);
        $data['buyOrder'] = $orderId;
        $aliOrder = model_api_aliApi::getOrderInfo($data);
        //如果有单号，就是抓取成功了，保存订单信息
        if(!empty($aliOrder['baseInfo']['idOfStr'])){
            $json = addslashes(json_encode($aliOrder));
            L::sql("UPDATE `pdd_order` 
            SET `aliData` = '{$json}' 
            WHERE
                `aliOrder` = '{$orderId}'");
            //如果有物流单号，就结束队列，然后加入标记发货的队列，否则60分钟后再抓取
            if(!empty($aliOrder['nativeLogistics']['logisticsItems'][0]['logisticsBillNo'])){
                L::sql("UPDATE `pdd_order` 
                SET `orderState` = '40' 
                WHERE
                    `aliOrder` = '{$orderId}'");
                //加入上传物流单号到PDD平台的消息队列
                of_base_com_mq::set(['pddOrderSend', $orderId], ['orderId' => $orderId]);
                return true;
            } else {
                return 3600;
            }
        }
        return false;
    }

}
return true;